package com.ivo.sys;

import java.io.IOException;
import java.net.InetAddress;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.RedirectView;

import com.ivo.core.WebConstants;
import com.ivo.sys.interceptor.WithoutLoginCheck;
import com.ivo.sys.user.User;
import com.ivo.sys.user.UserService;
import com.ivo.util.HttpRequestUtil;
import com.ivo.util.RequestUtil;
import com.ivo.util.WebUtils;

@RestController
@WithoutLoginCheck
public class LoginController {
	protected Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private UserService userService;
	
//	@RequestMapping("test.do")
//	public void test(HttpServletResponse response)throws IOException {
//		response.getWriter().print("test");
//	}
	
	@RequestMapping("login.do")  
	public ModelAndView login(HttpServletRequest request, HttpServletResponse response) throws IOException {
		ModelAndView mv = new ModelAndView();
		String username = request.getParameter("loginName");
		String password = request.getParameter("pass");
		
		Object attribute = WebUtils.getSessionAttribute(request,WebConstants.SessionUser);
		if (attribute != null && attribute instanceof User) {
			
			String factory = ((User) attribute).getFactory();
			if(factory.equals("ARY")) mv.setViewName("/sys/ary");
			if(factory.equals("CEL")) mv.setViewName("/sys/cel");
			if(factory.contains("M")) mv.setViewName("/sys/lcm");
			if(factory.equals("WH")) mv.setViewName("/sys/wh");
			return mv;
		}
		mv.setViewName(WebConstants.LoginView);
		if (!WebUtils.isFormSubmission(request))
			return mv;
		

		User user = userService.getUser(username.toUpperCase());
		if (user == null)
			return mv.addObject("loginName", username).addObject(WebConstants.ErrorAttribute, "工号不存在");
		
		if (!this.loginVerify(user, password))
			return mv.addObject("loginName", username).addObject(WebConstants.ErrorAttribute, "密码不正确！");
		
		HttpSession session = request.getSession();
		//存储session
		session.setAttribute(WebConstants.SessionUser, user);
		logger.info("[PMS Info] -->  Login User: " + user.getUser_id());
		logger.info("[PMS Info] -->  当前的路径: " + System.getProperty("user.dir"));
		
		try {
			String remoteAddr = RequestUtil.getIpAddr(request);
			if(remoteAddr.equals("0:0:0:0:0:0:0:1")) {
				InetAddress ia = InetAddress.getLocalHost();
				remoteAddr = RequestUtil.getLocalMac(ia);
			}
			String url = "http://myivo.ivo.com.cn/Myivo2/bpm/setLoginBasicInfo/"+remoteAddr+"/PMS/"+username.toUpperCase()+"";
			HttpRequestUtil.getHttpAsyncClient(url);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		
		String factory = user.getFactory();
		if(factory.equals("ARY")) mv.setViewName("/sys/ary");
		if(factory.equals("CEL")) mv.setViewName("/sys/cel");
		if(factory.contains("M")) mv.setViewName("/sys/lcm");
		if(factory.equals("WH")) mv.setViewName("/sys/wh");
		return mv;
		
	}
	public boolean loginVerify(String factory,User user) {
		if(user.getFactory().contains(factory))
			return true;
		else return false;
	}
	
	public boolean loginVerify(User user, String password) {
		if(password.equals(user.getPassword()))
			return true;
		else return false;
	}
	
	@RequestMapping(value = "logout.do")
	public ModelAndView logout(HttpServletRequest request, HttpSession session,RedirectAttributes redirectAttributes) {
		Object attribute = WebUtils.getSessionAttribute(request,WebConstants.SessionUser);
		if (attribute != null)
			redirectAttributes.addFlashAttribute("loginName",((User) attribute).getId());
		session.invalidate();
		return new ModelAndView(new RedirectView(WebUtils.makeUrl("login.do")));
	}
}
